﻿/*******************************************************************************
 * Copyright © 2016
 * 
 * Description: MVC快速开发平台  FROM http://xmwxgn.com
 *
*********************************************************************************/
using ZFine.Code;
using ZFine.Domain.Entity.Gas;
using ZFine.Domain.IRepository.Gas;
using ZFine.Repository.Gas;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ZFine.Application.Gas
{
    public class CompanyApp
    {
        private ICompanyRepository service = new CompanyRepository();

        public List<CompanyEntity> GetList(string keyword = "")
        {
            // 什么作用？
            var expression = ExtLinq.True<CompanyEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.CompanyName.Contains(keyword));
            }
            return service.IQueryable(expression).OrderBy(t => t.CompanyName).ToList();
        }
        public List<CompanyEntity> GetList(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<CompanyEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.CompanyName.Contains(keyword));
            }
            return service.FindList(expression, pagination);
        }
        public IQueryable<CompanyEntity> GetIQueryable(string keyword = "")
        {
            var expression = ExtLinq.True<CompanyEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.CompanyName.Contains(keyword));
            }
            return service.IQueryable(expression);
        }
        public int GetCount()
        {
            return service.IQueryable().Count();
        }
        public CompanyEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            //if (service.IQueryable().Count(t => t.F_ParentId.Equals(keyValue)) > 0)
            //{
            //    throw new Exception("删除失败！操作的对象包含了下级数据。");
            //}
            //else
            //{
            //    service.Delete(t => t.F_Id == keyValue);
            //}
        }
        public void SubmitForm(CompanyEntity organizeEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                organizeEntity.Modify(keyValue);
                service.Update(organizeEntity);
            }
            else
            {
                organizeEntity.Create();
                organizeEntity.CompanyPassword = "123456";
                service.Insert(organizeEntity);
            }
        }
    }
}
